using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using RollManager.Library;
using RollManager.Utils;
using RollManager.Library.Utils;

namespace RollManager.Forms.ActionPanels
{
    public partial class ActionControl : UserControl
    {
        private readonly DevLevel m_devLevel;
        private readonly RollManager.Library.Action m_action;
        private readonly SubAction m_subAction;
        private ActionWorker m_worker;
        private bool m_simulation;

        public TabPage ParentTab
        {
            get
            {
                return (TabPage) Parent;
            }
        }

        public ActionControl( DevLevel devLevel, RollManager.Library.Action action, SubAction subAction, bool simulation )
        {
            InitializeComponent();

            m_devLevel = devLevel;
            m_action = action;
            m_subAction = subAction;
            m_simulation = simulation;

            Name = string.Format( "{0}:{1}:{2}", m_devLevel.Name, m_action.Name, m_subAction.Name );

        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            // Set dialog title
            //Text = string.Format( "{0}:{1}:{2}", m_devLevel.Name, m_action.Name, m_subAction.Name );

            // start worker to run action steps
            m_worker = new ActionWorker( m_devLevel, m_subAction.Clone(), m_simulation );
            m_worker.ProgressChanged += worker_ProgressChanged;
            m_worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            m_worker.RunWorkerAsync();
        }

        private void worker_ProgressChanged( object sender, ProgressChangedEventArgs e )
        {
            // Add results from worker to output
            if( e.UserState is ServerAdvancement )
            {
                appendTabTitle( ( (ServerAdvancement) e.UserState ).Server );
            }
            else
            {
                resultsBox.Items.AddRange( (string[]) e.UserState );
                resultsBox.SelectedIndex = resultsBox.Items.Count - 1;
            }
        }

        private void worker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
        {
            string result;
            if( e.Cancelled )
            {
                result = "ABORTED";
                splitContainer1.Panel2.BackColor = Color.LightPink;
            }
            else
            {
                result = "FINISHED";
                splitContainer1.Panel2.BackColor = Color.DeepSkyBlue;

                // Add any remaining results to output
                string[] lastItems = e.Result as string[];
                if( lastItems != null )
                {
                    resultsBox.Items.AddRange( (string[]) e.Result );
                }
            }

            if( !m_simulation )
            {
                Database.AddHistory( m_subAction.Id, result, m_worker.Context );
            }

            // Change functionality of action button
            actionBtn.Enabled = true;
            actionBtn.Text = "&Close";
            actionBtn.Focus();

            appendTabTitle( "DONE" );
            ActionDialog.FlashWindow();
            ActionDialog.FocusTab( ParentTab );
        }

        private void actionBtn_Click( object sender, EventArgs e )
        {
            if( m_worker.IsBusy)
            {
                actionBtn.Enabled = false;
                actionBtn.Text = "Cancelling...";
                m_worker.CancelAsync();
            }
            else
            {
                ActionDialog.CloseTab( (TabPage) Parent );
            }
        }

        private void appendTabTitle(string message)
        {
            string[] pieces = ParentTab.Text.Split( '-' );
            ParentTab.Text = string.Format( "{0} - {1}", pieces[ 0 ].Trim(), message );
        }
    }
}